package 代码记录.Bit48天集训;

import java.util.Scanner;

/**1.解读密码
 * 2.走迷宫(搜索矩阵leetcode-13、day_38)
 * @author zx
 * @create 2022-07-01 21:20
 */
public class day_42 {
    public static void main2(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String line = sc.nextLine();
            String res = "";
            String[] str = line.split(" ");
            for(int i = 0;i < str.length;i++){
                for(int j = 0;j < str[i].length();j++){
                    if(str[i].charAt(j) >= '0' && str[i].charAt(j) <= '9'){
                        res += String.valueOf(str[i].charAt(j));
                    }
                }
            }
            System.out.println(res);
        }
    }


    /**
     * 还得再看看,不太懂！
     */
    private static boolean[][] used = new boolean[10][10];
    private static int res = Integer.MAX_VALUE;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            res = Integer.MAX_VALUE;
            char[][] map = new char[10][10];
            for(int i = 0;i < 10;i++){
                String line = sc.nextLine();
                for(int j = 0;j < 10;j++){
                    map[i][j] = line.charAt(j);
                }
            }
            dfs(map,0,1,0);
            System.out.println(res);
        }
    }
    //res存放最短路径,temp存放当前路径
    private static void dfs(char[][] map, int x, int y, int temp) {
        if (x < 0 || y < 0 || used[x][y] || x >= 10 || y >= 10 || map[x][y] == '#') {
            return;
        }
        if (temp > res) {
            return;//这个地方的剪枝很关键!!! 在路径很多的时候,可以有效的防止多余的计算
        }
        if (x == 9 && y == 8) {
            if (temp < res) {
                res = temp;//存储最短路径
            }
            return;
        }
        used[x][y] = true;
        dfs(map, x - 1, y,temp + 1);
        dfs(map, x, y - 1,temp + 1);
        dfs(map, x + 1, y,temp + 1);
        dfs(map, x, y + 1,temp + 1);
        used[x][y] = false;
    }
}
